package com.peng.commonlibrary.allkindsoflist.common;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;

/**
 * Created by M_genius on 2018/1/29 14:50.
 * 描述：
 *      SparseArray 避免自动装箱、添加数据时，使用二分查找的方式排序。采用压缩的方式来表示稀疏数组的数据
 */

public class SuperViewHolder extends RecyclerView.ViewHolder {

    private SparseArray<View> views;


    public SuperViewHolder(View itemView) {
        super(itemView);
        this.views = new SparseArray<>();
    }


    /**
     * 该方法为一个泛型方法
     *
     * @param viewId
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T extends View> T getView(int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }
}